using System;
using System.Collections.Generic;
using System.Text;

using LuaInterface;

namespace miciv
{
    public class LuaManager
    {
        protected CoreContext m_coreContext;
        protected Lua m_lua;

        public Lua State
        {
            get
            {
                return this.m_lua;
            }
        }

        public LuaManager()
        {
        }

        public bool Initialize(CoreContext _coreContext)
        {
            this.m_coreContext = _coreContext;
            this.m_lua = new Lua();
            return true;
        }

        public bool DoFile(string _strLocation)
        {
            bool bResult = true;
            try
            {
                this.m_lua.DoFile(this.m_coreContext.CoreManager.GetExistingFile(_strLocation));
            }
            catch (LuaException e)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("could not load : {0}", _strLocation));
                System.Diagnostics.Debug.WriteLine(e.Message);
                bResult = false;
            }
            return bResult;
        }

        public LuaTable GetTable(string _strPath)
        {
            try
            {
                return this.m_lua.GetTable(_strPath);
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("could not access table : {0}", _strPath));
                System.Diagnostics.Debug.WriteLine(e.Message);
                return null;
            }
        }

        public LuaFunction GetFunction(string _strPath)
        {
            try
            {
                return this.m_lua.GetFunction(_strPath);
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("could not access function : {0}", _strPath));
                System.Diagnostics.Debug.WriteLine(e.Message);
                return null;
            }
        }
    }
}
